<?php

class CartController extends Controller
{
	public function actionIndex()
	{
        $positions = Yii::app()->shoppingCart->getPositions();
		$this->render('index',array('sanPham'=>$positions));
	}

    public  function actionAddCart($maSp,$slg)
    {
        $sanPham = SanPham::model()->findByPk($maSp);
        Yii::app()->shoppingCart->put($sanPham,$slg);
        $positions = Yii::app()->shoppingCart->getPositions();
        $this->render('index',array('sanPham'=>$positions));

        //$this->render('../site/index');
        //$this->render('index',array('sanPham'=>$sanPham,'masp'=>$maSp));
    }

    public  function actionRemoveCart($maSp)
    {
        $sanPham = SanPham::model()->findByPk($maSp);
        Yii::app()->shoppingCart->remove($sanPham->getId());;
        $positions = Yii::app()->shoppingCart->getPositions();
        $this->render('index',array('sanPham'=>$positions));
    }

    public  function actionUpdateCart($maSp)
    {
        $ssl = $_POST["SoLuong"];
        $sanPham = SanPham::model()->findByPk($maSp);
        Yii::app()->shoppingCart->update($sanPham,$ssl);

        $positions = Yii::app()->shoppingCart->getPositions();
        $this->render('index',array('sanPham'=>$positions));

        //$this->render('../site/index');
        //$this->render('index',array('sanPham'=>$sanPham,'masp'=>$maSp));

    }

    public  function actionPayment()
    {
        $this->render('payment');
    }
    public function  actionPay()
    {
        $positions = Yii::app()->shoppingCart->getPositions();

        $user = new Khachhang();
        $user->MAKH= "temp";
        $user->TENKH=$_POST["TENKH"];
        $user->DIACHI=$_POST["DIACHI"];
        $user->GHICHU=$_POST["GHICHU"];
        $user->save();
        $user->MAKH=$user->IDKH;
        $user->save();


        $hoadon = new Hoadon();
        $hoadon->MAHD="temp";
        $hoadon->NGAYLAP="2012";
        //$hoadon->NGAYTT=Yii::app()->getDateFormatter()->toString();
        $hoadon->NGAYTT="2012";
        $hoadon->IDKH=$user->IDKH;
        $hoadon->save();
        $hoadon->MAHD=$hoadon->IDHD;
        $hoadon->save();


        foreach($positions as $sp )
        {
            $chiTietHd = new Cthoadon();
            $chiTietHd->IDHD = $hoadon->IDHD;
            $chiTietHd->IDSP= $sp->IDSP;
            $chiTietHd->SOLUONG=$sp->getQuantity();
            $chiTietHd->save();
        }

        Yii::app()->shoppingCart->clear();

        $this->render('Success_Payment',array('maKH'=>$user->IDKH,'maHD'=>$hoadon->IDHD));

    }
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}